package com.qq.mobilemanager.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qq.mobilesafe.R;
import com.qq.mobilesafe.base.adapter.ViewHolder;
import com.qq.mobilesafe.base.adapter.recyclerview.CommonAdapter;
import com.qq.mobilesafe.base.adapter.recyclerview.OnItemClickListener;
import com.qq.mobilesafe.domain.ToolsInfo;
import com.qq.mobilesafe.manager.ThreadManager;
import com.qq.mobilesafe.utils.PackageUtils;
import com.qq.mobilesafe.utils.SmsUtils;
import com.qq.mobilesafe.utils.SmsUtils.BackUpCallbackSms;

public class AToolActivity extends Activity {
	// @ViewInject(R.id.gridview)
	// private GridView gridView;

	private List<ToolsInfo> list;

	private ProgressDialog pd;

	@ViewInject(R.id.progressBar)
	private ProgressBar progressBar;

	private GridAdapter adapter;
	@ViewInject(R.id.gridview)
	private RecyclerView recyclerView;

	// private ToolsAdapter adapter;

	private String[] toolsName = new String[] { "归属地查询", "短信备份", "联系人备份",
			"软件锁", "图案密码" };
	private int[] iconId = new int[] { R.drawable.home_depot, R.drawable.sms,
			R.drawable.contact, R.drawable.ic_launcher, R.drawable.ic_launcher };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initData();
		initView();

		GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
		recyclerView.setLayoutManager(layoutManager);
		adapter = new GridAdapter(this,R.layout.item_tools , list);
		recyclerView.setAdapter(adapter);
		adapter.setOnItemClickListener(new OnItemClickListener<ToolsInfo>() {

			@Override
			public void onItemClick(ViewGroup parent, View view, ToolsInfo t,
					int position) {
				switch (position) {
				case 0:
					PackageUtils.startActivity(AToolActivity.this,
							AppLockActivity.class);
					break;
				case 1:
					PackageUtils.startActivity(AToolActivity.this,
							QueryAddressActivity.class);
					break;
				case 2:
					PackageUtils.startActivity(AToolActivity.this,
							AppLockActivity.class);
					break;
				case 3:
					backUpsms();
					break;
				}
			}

			@Override
			public boolean onItemLongClick(ViewGroup parent, View view,
					ToolsInfo t, int position) {
				return false;
			}
		});
	}

	private void initData() {
		list = new ArrayList<ToolsInfo>();

		for (int i = 0; i < iconId.length; i++) {
			ToolsInfo info = new ToolsInfo();
			info.icon = iconId[i];
			info.toolName = toolsName[i];
			list.add(info);
		}

	}

	private void initView() {
		setContentView(R.layout.activity_atools);
		ViewUtils.inject(this);
	}

	public void addressQuery(View view) {
		startActivity(new Intent(this, QueryAddressActivity.class));
	}

	public void appLock(View view) {
		startActivity(new Intent(this, AppLockActivity.class));
	}

	private void backUpsms() {

		pd = new ProgressDialog(AToolActivity.this);
		pd.setTitle("提示");
		pd.setMessage("稍安勿躁。正在备份。你等着吧。。");
		pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		pd.show();
		ThreadManager.getInstance().createShortPool().execute(new Runnable() {

			@Override
			public void run() {
				boolean result = SmsUtils.backUp(AToolActivity.this,
						new BackUpCallbackSms() {

							@Override
							public void onBackUpSms(int progress) {
								pd.setProgress(progress);
								progressBar.setProgress(progress);
							}

							@Override
							public void before(int count) {
								pd.setMax(count);
								progressBar.setMax(count);

							}
						});
				pd.dismiss();
			}
		});

	}
	
	private class GridAdapter extends CommonAdapter<ToolsInfo>{

		public GridAdapter(Context context, int layoutId, List<ToolsInfo> datas) {
			super(context, layoutId, datas);
		}

		@Override
		public void convert(ViewHolder holder, ToolsInfo t) {
			holder.setImageResource(R.id.iv_tools, t.icon);
			holder.setText(R.id.tv_toolName, t.toolName);
		}
		
	}
}
